package com.laboratory.util.json;

import com.laboratory.util.json.JSONUtil;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import  com.laboratory.util.office.ExcelUtil;
import javafx.beans.binding.ObjectExpression;

/**
 * Created by 30868 on 2019/05/13.
 */
public class ReadJson {
    /**
     * 以列表的方式获取文件的所有行
     *
     * @param file     需要处理的文件
     * @param encoding 指定读取文件的编码
     * @return 包含所有行的list
     */
    public final static String readFile(File file, String encoding) {
       StringBuilder stringBuilder = new StringBuilder();

        try (
                BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding))
        ) {
            String line;
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

    /**
     * 大json字符文件，转换为excel表格
     */
    public static  void jsontoExcel() throws Exception {
        String str = readFile(new File("E:\\文件\\批量导入问题\\ngcs.yn.cs.cmos-1557430844644-900+.log"), "UTF-8");
        System.out.println(str.length());
        List<Map<String, Object>> mapList = JSONUtil.jsonToListMap(str);
        System.out.println(mapList.size());
        List<List<String>> resultList = new ArrayList<>();

        for (Map<String, Object> map: mapList
                ) {
            List<String> strList = new ArrayList<>();
            strList.add( (String)map.get("SRV_REQST_TYPE_FULL_NM"));
            strList.add( (String)map.get("BIZ_BELG_DEPT_NM"));
            resultList.add(strList);
        }

        String filePath = "E:\\文件\\批量导入问题\\业务归属部门为空-附件未包含-从导入日志查到归属部门.xlsx";
        ExcelUtil.writeExcel2007(resultList, filePath);
    }

    public static void main(String[] args) throws Exception {
        String filePath = "E:\\文件\\批量导入问题\\节点2.xlsx";
        HashMap<String, Object> resultMap = new HashMap<>();
        List<List<String>> resultList = new ArrayList<>();
        ExcelUtil.readExcel2007(filePath, "Sheet0", resultList);
        System.out.println("节点2 total：" + resultList.size());
        for(int i = 0; i<resultList.size(); i++){
            List<String> result = resultList.get(i);
            if (resultMap.containsKey(result.get(0))){
                System.out.println("节点2 Excel 有重复的节点 " + i) ;
            }
            resultMap.put(result.get(0), result);
        }
        System.out.println("节点2 resultMap大小：" + resultMap.size());


        HashMap<String, Object> nodeMap = new HashMap<>();
        List<List<String>> NodeeList = new ArrayList<>();
        ExcelUtil.readExcel2007("E:\\文件\\批量导入问题\\业务归属部门为空-附件未包含-从导入日志查到归属部门.xlsx", "Sheet0", NodeeList);
        System.out.println( "total : "+NodeeList.size());
        for(int i = 0; i<NodeeList.size(); i++){
            String NodeNm = NodeeList.get(i).get(0);
            if (nodeMap.containsKey(NodeNm)) {
                System.out.println("此行节点已经存在： "+i);
            }
            nodeMap.put(NodeNm, NodeeList.get(i));
        }
//        Map<String, Object> notContainMap = new HashMap<>();
        List<String> notContainList = new ArrayList<>();
        for (Map.Entry<String, Object> result: resultMap.entrySet()) {
            String nodeNm = result.getKey();
            List<String> value = (List<String>) result.getValue();
            String deptNm = value.get(1);
            if (deptNm!=null && !deptNm.equals("")){
                continue;
            }

            List<String> stringList = (List<String>) nodeMap.get(nodeNm);
            if (!nodeMap.containsKey(nodeNm) || (stringList== null || stringList.size() ==0)){
                notContainList.add(result.getKey());
                System.out.println(result.getKey());
            }
       }

        System.out.println("notContainList.size "+notContainList.size());


    }




}
